<?php
namespace view;

class CookieView {
    private static $m_cookieLength = 3600;
    private static $m_cookieNameUsername = "LaylasKaka1";
    private static $m_cookieNamePassword = "LaylasKaka2";
    private $loginModel;


    /**
    * @param /controller/LoginController $loginModel
    */
    public function __construct(\model\LoginModel $loginModel) {
        $this->loginModel = $loginModel;
    }


    /**
    * @return bolean
    *
    * kollar om vi har några kakor som är satta i vår superglobala array $_COOKIE
    */
    public function hasCookies() {
        if (isset($_COOKIE[self::$m_cookieNameUsername]) && isset($_COOKIE[self::$m_cookieNamePassword])) {
            return true;
        } else {
                return false;
        }
    }


    /**
    * @return array
    *
    * här hämtas våra kakor ut och skickas tillbaka som en array
    */
    public function getCookies() {
        if (isset($_COOKIE[self::$m_cookieNameUsername])) {
           return array ($_COOKIE[self::$m_cookieNameUsername], $this->loginModel->encryptionModel->deCrypt($_COOKIE[self::$m_cookieNamePassword]), "1");
        }
    }


    /**
    * här sätter vi våra kakor
    */
    public function setCookies() {
        if($this->loginModel->m_userWantsToBeRemebered) {
            $enddate = time()+self::$m_cookieLength;
           setcookie(self::$m_cookieNameUsername, $this->loginModel->userModel->returnValidUsername(),  $enddate);
           setcookie(self::$m_cookieNamePassword, $this->loginModel->encryptionModel->enCrypt($this->loginModel->userModel->returnValidPassword()), $enddate);
        }
    }


    /**
    * här tas våra kakor bort
    */
    public function resetCookies() {
        if (isset($_COOKIE[self::$m_cookieNameUsername]) && isset($_COOKIE[self::$m_cookieNamePassword])) {
            setcookie(self::$m_cookieNameUsername, "", time() -3600);
            setcookie(self::$m_cookieNamePassword, "", time() -3600);
        }
    }


}


